#include "ApplicationView.h"

#include <boost/bind.hpp>
#include "../../../utils/LateEvents.hpp"

using namespace Views::Graphic;
using namespace Views::Helpers::Graphic::Application;
using namespace Utils;

ApplicationView::ApplicationView(GUI::Form* form)
	: BaseGraphicView(form), game_mode_dialog(NULL), game_end_dialog(NULL)
{
}

ApplicationView::~ApplicationView()
{
	delete game_end_dialog;
}

void ApplicationView::openGameModeDialog()
{
	game_mode_dialog = new GameModeDialog();
	Form->addChild(game_mode_dialog);

	game_mode_dialog->doOnAction(LateAppLoopEventFactory::create(&ApplicationView::onGameModePick, this));
	game_mode_dialog->MoveCenter();
	Form->Draw(true);
}

void ApplicationView::onGameModePick(Domain::Configuration::GAME_MODE mode)
{
	delete game_mode_dialog;
	game_mode_dialog = NULL;
	gameModePickEvent(mode);
}

void ApplicationView::gameLost(Players::Player* player, Cards::CardSet& cards, Domain::Configuration::GAME_END_REASON reason)
{
	game_end_dialog = new GameEndDialog(cards);
	if (reason == Domain::Configuration::GAME_END_DEDUCTED)
		game_end_dialog->gameLostDeducted();
	else if(reason == Domain::Configuration::GAME_END_COMPETITORS_LOST)
		game_end_dialog->gameLostOtherFailed();
	Form->addChild(game_end_dialog);
	game_end_dialog->MoveCenter();
	Form->Draw(true);
}

void ApplicationView::gameWon(Players::Player* player, Cards::CardSet& cards, Domain::Configuration::GAME_END_REASON reason)
{
	game_end_dialog = new GameEndDialog(cards);
	if (reason == Domain::Configuration::GAME_END_DEDUCTED)
		game_end_dialog->gameWonDeducted();
	else if(reason == Domain::Configuration::GAME_END_COMPETITORS_LOST)
		game_end_dialog->gameWonOtherFailed();
	Form->addChild(game_end_dialog);
	game_end_dialog->MoveCenter();
	Form->Draw(true);
}

